package ControlLayer;
import ModelLayer.*;
import java.util.ArrayList;

public class ReservationCtr
{
    private AddressCtr addressCtr;
    private DVDCtr dvdCtr;
    private ReservationLibrary resvLib;

    public ReservationCtr()
    {
        addressCtr = new AddressCtr();
        dvdCtr = new DVDCtr();
        resvLib = ReservationLibrary.getReservationLib();
    }

    public boolean createReservation(int _resvId, String _resvDate, int _personId, int _dvdId)
    {
        boolean success = false;
        
        Person person = addressCtr.findPerson(_personId);
        DVD dvd = dvdCtr.getDvdById(_dvdId);
        
        if(person != null && dvd != null)
        {
            Reservation newResv = new Reservation(_resvId, _resvDate, person, dvd);
            
            resvLib.addReservation(newResv);
            
            success = true;
        }
        
        return success;
    }
    
    public boolean deleteReservation(int _resvId)
    {
        boolean success = false;
        Reservation _resv = resvLib.getReservationById(_resvId);
        
        if(_resv != null)
        {
            resvLib.removeReservation(_resv);
            success = true;
        }
        
        return success;
    }
    
    public String getReservationInfoById(int _id)
    {
        String retStr = "";
        Reservation resv = resvLib.getReservationById(_id);
        
        if(resv != null)
        {
            retStr = " -----------------------" + "\n";
            retStr += " Reservation Info:" + "\n";
            retStr += " Id: " + resv.getReservationId() + "\n";
            retStr += " Date: " + resv.getReservationDate() + "\n";
            retStr += " Person Name: " + resv.getReservationPerson().getName() + "\n";
            retStr += " Dvd name: " + resv.getReservationDvd().getTitle() + "\n";
            retStr += " -----------------------";
        }
        
        return retStr;
    }
    
    public String listAllReservations()
    {
        String retStr = "";
        
        ArrayList<Reservation> lstReservations = resvLib.getAllReservations();
        
        if(lstReservations != null)
        {
            retStr = " -----------------------" + "\n";
            retStr += " Reservation List:" + "\n";
            for(Reservation resv : lstReservations)
            {   
                retStr += " Id: " + resv.getReservationId() + "- Date: " + resv.getReservationDate() + "- Person Name: " 
                + resv.getReservationPerson().getName() + "- Dvd Title" + resv.getReservationDvd().getTitle() + "\n";   
            }
            retStr += " -----------------------";
        }
        
        return retStr;
    }
    
    public String getPersonInfo(int _id)
    {
        return addressCtr.getPerson(_id);
    }
    
    public String getDvdInfo(int _dvd)
    {
        return dvdCtr.findDVD(_dvd);
    }
}
